package com.triptogether.utils.googleplayservices;

import android.app.Activity;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.triptogether.utils.log.Logger;

public class GooglePlayServicesUtils {

	private static final String TAG = Logger.makeLogTag(GooglePlayServicesUtils.class);
	private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

	/**
	 * Check the device to make sure it has the Google Play Services APK. If it
	 * doesn't, display a dialog that allows users to download the APK from the
	 * Google Play Store or enable it in the device's system settings.
	 * 
	 * @param currentActivity
	 *            Current {@link Activity} that work.
	 * @return <code>false</code> if the current device not supports on Google
	 *         Play Services, otherwise <code>true</code>
	 */
	public static boolean checkPlayServices(Activity currentActivity) {
		int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(currentActivity);
		if (resultCode != ConnectionResult.SUCCESS) {
			if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
				GooglePlayServicesUtil.getErrorDialog(resultCode, currentActivity,
				                                      PLAY_SERVICES_RESOLUTION_REQUEST).show();
			} else {
				Log.i(TAG, "This device is not supported.");
				currentActivity.finish();
			}
			return false;
		}
		return true;
	}
}
